package com.lujianing.controller;

import com.lujianing.model.Article;
import com.lujianing.page.Page;
import com.lujianing.service.ArticleService;
import com.lujianing.util.JsonUtil;
import com.qiniu.Uptoken;
import com.qiniu.api.auth.AuthException;
import org.json.JSONException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Controller
public class BaseController {

    @Autowired
    private ArticleService articleService;

	@RequestMapping(value="/",method = RequestMethod.GET)
	public String printWelcome(ModelMap model) {
        Page<Article> pages= articleService.listPage(1,5);
		model.addAttribute("pages", pages);
		return "base/index";
	}

    @RequestMapping(value="/baidu",method = RequestMethod.GET)
    public String printBaidu(ModelMap model) {
        return "base/baidu";
    }

    @RequestMapping(value="/baidu_controller",method = RequestMethod.GET)
    public String baiduController(ModelMap model) {
        return "base/baidu";
    }

    @RequestMapping(value="/qiniu_token",method = RequestMethod.GET)
    public void qiniu_token(ModelMap model,HttpServletRequest request,HttpServletResponse response) throws IOException, AuthException, JSONException {
        Map map = new HashMap();
        map.put("token",Uptoken.getUpToken());
        response.getWriter().write(JsonUtil.toJson(map));
    }
}